package views.admin;

import tools.Icons;
import utils.DBUtil;
import views.SystemRootFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 管理员修改自己登录密码的界面
 */
public class AdminChangePassword extends JFrame {
    private JLabel jL_oldpass = new JLabel("原  密  码：");
    private JLabel jL_newpass = new JLabel("新  密  码：");
    private JLabel jL_repeatpwd = new JLabel("重复输入：");
    private JLabel JL_changepass = new JLabel("修  改  密  码");
    private JPasswordField JP_oldpass = new JPasswordField();
    private JPasswordField JP_newpass = new JPasswordField();
    private JPasswordField JP_repeatpass = new JPasswordField();
    private JPanel panLab = new JPanel();
    private JPanel panBtn = new JPanel();
    private JButton jB_ok = new JButton("确 定");
    private JButton jB_repeat = new JButton("重 置");
    ImageIcon imageIcon = new ImageIcon("src/images/admin.png");
    Icons icons = new Icons();

    public AdminChangePassword() {
        setTitle("修改密码-管理员");
        setSize(400, 300);
        setResizable(false); // 不可改变窗口大小
        setIconImage(imageIcon.getImage().getScaledInstance(80, 80, Image.SCALE_DEFAULT));
        // 获取屏幕大小和当前frame的大小
        Dimension thisScreen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension thisFrame = this.getSize();
        // 使启动窗口位于屏幕的正中心
        setLocation((thisScreen.width - thisFrame.width) / 2,
                (thisScreen.height - thisFrame.height) / 2);
        // 设置单击窗口的【关闭】按钮时将发生相应的动作
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        JL_changepass.setFont(new Font("TsangerYuMo W03", Font.BOLD, 26));
        JL_changepass.setHorizontalAlignment(JLabel.CENTER);
        JL_changepass.setForeground(new Color(255, 51, 51));

        jL_oldpass.setSize(100, 100);
        jL_oldpass.setLocation(50, 5);
        jL_oldpass.setHorizontalAlignment(JLabel.RIGHT);
        JP_oldpass.setSize(150, 30);
        JP_oldpass.setLocation(160, 40);

        jL_newpass.setSize(100, 100);
        jL_newpass.setLocation(50, 60);
        jL_newpass.setHorizontalAlignment(JLabel.RIGHT);
        JP_newpass.setSize(150, 30);
        JP_newpass.setLocation(160, 95);

        jL_repeatpwd.setSize(100, 100);
        jL_repeatpwd.setLocation(50, 115);
        jL_repeatpwd.setHorizontalAlignment(JLabel.RIGHT);
        JP_repeatpass.setSize(150, 30);
        JP_repeatpass.setLocation(160, 150);

        panLab.setLayout(null);
        panLab.add(jL_oldpass);
        panLab.add(jL_newpass);
        panLab.add(jL_repeatpwd);
        panLab.add(JP_oldpass);
        panLab.add(JP_newpass);
        panLab.add(JP_repeatpass);

        panBtn.add(jB_ok);
        panBtn.add(jB_repeat);

        add(JL_changepass, BorderLayout.NORTH);
        add(panBtn, BorderLayout.SOUTH);
        add(panLab, BorderLayout.CENTER);

        jB_ok.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                String Old = new String(JP_oldpass.getPassword()).trim();
                String New = new String(JP_newpass.getPassword()).trim();
                String Again = new String(JP_repeatpass.getPassword()).trim();
                int account = SystemRootFrame.ADMIN_ACCOUNT;
                Statement stmt = DBUtil.getStatement();
                String sql = "update admin set password ='" + New + "' where account = " + account + ";";
                String sql2 = "select * from admin where account=" + account + " AND password='" + Old + "';";
                try {
                    ResultSet rs = stmt.executeQuery(sql2);
                    if (rs.next()) {
                        if (!New.equals(Again)) {
                            JOptionPane.showMessageDialog(null, "两次密码输入不一致！", null, JOptionPane.ERROR_MESSAGE, icons.getErrorImage());
                        } else {
                            stmt.executeUpdate(sql);
                            JOptionPane.showMessageDialog(null, "密码修改成功。", null, JOptionPane.INFORMATION_MESSAGE, icons.getOkImage());
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "原密码输入错误！", null, JOptionPane.ERROR_MESSAGE, icons.getErrorImage());
                    }
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        });

        jB_repeat.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                JP_oldpass.setText("");
                JP_newpass.setText("");
                JP_repeatpass.setText("");
            }
        });
    }
}
